#!/bin/bash

date=$(date +%F_%H%M%S)
OUTDIR=/tmp/log-capture-${date}
ARCHIVE=${1-/tmp/log-capture.tar.bz2}

echo -e "Generating ${ARCHIVE}, please wait.\n"

# shellcheck disable=SC2068
_to_log() { local OutputFile=$(tr ' /' '_' <<<"$@"); $@ >${OUTDIR}/${OutputFile}; }

mkdir -p ${OUTDIR}
cd ${OUTDIR}

if [[ ! -f /tmp/ks.cfg ]];then
  echo "# No /tmp/ks.cfg present" > ${OUTDIR}/ks.cfg
else
    _to_log cat /mnt/sysimage/root/ks.cfg
fi

# the /mnt/sysimage/root/anaconda-ks.cfg file
# might not yet exist when this script is called
if [[ ! -f  /mnt/sysimage/root/anaconda-ks.cfg ]];then
    echo "# No /mnt/sysimage/root/anaconda-ks.cfg" > ${OUTDIR}/anaconda-ks.cfg
else
    _to_log cat /mnt/sysimage/root/anaconda-ks.cfg
fi

_to_log date
_to_log dmesg
_to_log dmidecode
_to_log lspci -vvnn

for DEV_NAME in /sys/block/*
do
    DEV_NAME=$(basename "$DEV_NAME")
    _to_log blkid --probe /dev/${DEV_NAME}
done
for PART in $(blkid | cut -d':' -f1)
do
    _to_log blkid --probe ${PART}
done

_to_log ls -lR /dev
_to_log parted -l
_to_log lsblk --all --topology
_to_log lsblk --output-all --json
_to_log blkid --output full
_to_log dmsetup ls --tree
_to_log lvm pvs
_to_log lvm vgs
_to_log lvm lvs
_to_log cat /proc/devices
_to_log cat /proc/mdstat
_to_log cat /proc/partitions
_to_log mount
_to_log df -h
_to_log cat /proc/meminfo
_to_log cat /proc/cpuinfo
_to_log ps axf
_to_log lsof
_to_log ip -s li
_to_log ip a
_to_log ip r
_to_log journalctl

cp /etc/resolv.conf ${OUTDIR}

if [[ -e /sys/firmware/efi ]]; then
    _to_log ls -l /sys/firmware/efi/efivars
fi

find /tmp -type f -name '*.log' -exec cp '{}' ${OUTDIR} ';'

if [[ -e /root/lorax-packages.log ]];then
    cp /root/lorax-packages.log ${OUTDIR}
fi

if [[ -e /tmp/pre-anaconda-logs/ ]];then
    cp -r /tmp/pre-anaconda-logs ${OUTDIR}
fi

tar cfa ${ARCHIVE} -C / ./${OUTDIR}

echo -e "\nFinished gathering data\nUpload ${ARCHIVE} to another system\n"
